<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" @click="handleForm(false, {})" :disabled="isDisabledAuth(Auth.saveMchQrcode)">
          <Icon icon="i-ant-design:plus-outlined" /> {{ t("新增") }}
        </a-button>
      </template>

      <template #id="{ record }">
        <div class="flex justify-center cursor-pointer" @click="handlePreview(record)">
          <div class="mr-2">
            <QrcodeOutlined />
          </div>
          <a>{{ record.id }}</a>
        </div>
      </template>

      <template #bindInfo="{ record }">
        <span class="cursor-pointer" @click="expandCollapse(record)">
          {{ record.areaName }}
        </span>
      </template>
    </BasicTable>
    <InputForm @register="registerInputForm" @success="handleSuccess" />
    <FormDetail @register="registerFormDetail" />
    <QrcodeBindInfo @register="registerBindInfo" @success="handleSuccess" />
    <Image
      v-if="visible"
      :src="imagePreviewBase64"
      :style="{ display: 'none' }"
      :preview="{
        visible,
        onVisibleChange: setVisible,
      }"
    />
  </div>
</template>
<script lang="ts" name="MchQrcode" setup>
  import { ref } from "vue"
  import { Auth } from "/@/enums/authEnum"
  import { Image } from "ant-design-vue"
  import { ImagePreview } from "/@/components/Preview/index"
  import { QrcodeOutlined } from "@ant-design/icons-vue"
  import { BasicTable, BasicColumn, useTable } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useI18n } from "/@/hooks/web/useI18n"
  import { usePermission } from "/@/hooks/web/usePermission"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns, searchFormSchema } from "./qrcode.data"
  import { pageMchQrcode, deleteMchQrcode, getMchQrcodeView } from "/@/api"
  import { useMessage } from "/@/hooks/web/useMessage"
  import QrcodeBindInfo from "./components/QrcodeBindInfo.vue"
  import InputForm from "./QrcodeForm.vue"
  import FormDetail from "./QrcodeDetail.vue"
  import { useRouter } from "vue-router"
  import { Icon } from "/@/components/Icon"

  const { t } = useI18n("")
  const { createMessage } = useMessage()
  const glob = useGlobSetting()
  const { isDisabledAuth } = usePermission()
  const [registerBindInfo, { openDrawer: openQrcodeBindInfo }] = useDrawer()
  const [registerInputForm, { openDrawer: openInputForm }] = useDrawer()
  const [registerFormDetail, { openDrawer: openFormDetail }] = useDrawer()
  const visible = ref<boolean>(false)
  const imagePreviewBase64 = ref()
  const actionColumn: BasicColumn = {
    width: 160,
    title: "操作",
    dataIndex: "action",
    fixed: "right",
    slot: "tableActions",
    actions: (record: Recordable) => [
      {
        label: "删除",
        color: "error",
        ifShow: record.sendStatus != 1,
        auth: Auth.deleteMchQrcode,
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: "编辑",
        auth: Auth.updateMchQrcode,
        onClick: handleForm.bind(null, true, record),
      },
    ],
    dropDownActions: (record: Recordable) => [
      {
        label: "详情",
        onClick: handleDetail.bind(null, record),
      },
      {
        label: "绑定",
        onClick: handleOpenBindInfo.bind(null, record),
      },
    ],
  }

  const [registerTable, { reload, expandAll, collapseAll, expandCollapse }] = useTable({
    title: "商户码牌",
    api: pageMchQrcode,
    columns: columns,
    bordered: false,
    beforeFetch: (params) => {
      return params
    },
    formConfig: {
      baseColProps: { lg: 6, md: 12 },
      labelWidth: 90,
      schemas: searchFormSchema,
      showResetButton: false,
    },
    showTableSetting: true,
    showIndexColumn: false,
    useSearchForm: true,
    pagination: true,
    canResize: true,
    rowKey: "id",
    actionColumn,
  })

  const handleForm = (isUpdate, record) => openInputForm(true, { record, isUpdate })
  const handleDelete = async (record) => await deleteMchQrcode({ id: record.id }, reload)
  const handleDetail = (record) => openFormDetail(true, { record })
  const handleOpenBindInfo = (record) => openQrcodeBindInfo(true, { record })
  const handleSuccess = () => reload()
  const setVisible = (value): void => {
    visible.value = value
  }
  const handlePreview = async (record) => {
    imagePreviewBase64.value = await getMchQrcodeView({ qrcodeId: record.id })
    visible.value = true
  }
</script>

<style lang="less" scoped></style>
